package cn.ehanghai.iot.back.controller;

import cn.ehanghai.iot.back.domain.UserInfo;
import cn.ehanghai.iot.back.domain.UserMessage;
import cn.ehanghai.iot.back.service.UserInfoService;
import cn.ehanghai.iot.back.service.UserMessageService;
import cn.ehanghai.iot.common.controller.BaseController;
import cn.ehanghai.iot.common.domain.PageSet;
import cn.ehanghai.iot.common.security.constants.Constants;
import cn.ehanghai.iot.common.security.constants.ReturnCode;
import cn.ehanghai.iot.config.DefineUtil;
import cn.ehanghai.iot.dataobject.IdCard;
import cn.ehanghai.iot.idcard.IdCardUtil;
import cn.ehanghai.iot.sms.SendMessage;
import cn.ehanghai.iot.utils.InviteCodeGnerateUtil;
import com.aliyuncs.exceptions.ClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;


/**
 * @Author: xumf
 * @Date: 2019/6/26 17:30
 */
@RequestMapping("user")
@RestController
public class UserInfoController extends BaseController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserMessageService userMessageService;

    @Autowired
    private RedisTemplate<String,String> template;

    /**
     * 获取列表
     * @param pageNum
     * @param pageSize
     * @param phone
     * @param status
     */
    @RequestMapping(value = "list",method = RequestMethod.POST)
    public ReturnCode list(@RequestBody Map<String, String> map){
        String pageNum=map.get("pageNum");
        String pageSize=map.get("pageSize");
        String phone=map.get("phone");
        String status=map.get("status");
        String province=map.get("province");
        PageSet list=userInfoService.selectUserInfoListByPage(Integer.valueOf(pageNum),Integer.valueOf(pageSize),phone,status,province);
        return success(list);
    }


    /**
     * 查看留言
     * @param userId
     */
    @RequestMapping(value = "message",method = RequestMethod.GET)
    public ReturnCode viewMessage(Long userId){
        UserMessage userMessage=userMessageService.selectUserMessageByUserId(userId);
        String content=null;
        if(userMessage!=null){
            content=userMessage.getContent();
        }
        Map<String,Object> map=new HashMap<>();
        map.put("content",content);
        return success(map);
    }


    /**
     * 用户邀请列表
     * @param pageNum
     * @param pageSize
     * @param userId
     */
    @RequestMapping(value = "inviteList",method = RequestMethod.POST)
    public ReturnCode InviteList(@RequestBody Map<String, String> map){
        String pageNum=map.get("pageNum");
        String pageSize=map.get("pageSize");
        String userId=map.get("userId");
        PageSet list= userInfoService.selectinviteListListByPage(Integer.valueOf(pageNum),Integer.valueOf(pageSize),Long.valueOf(userId));
        return success(list);
    }

    //-------公众号接口-------------

    /**
     * 获取验证码
     * @param phone
     *
     */
    @RequestMapping(value = "getCode",method = RequestMethod.POST)
    public ReturnCode  getCode(@RequestBody Map<String,Object> map){
        String msgCode=InviteCodeGnerateUtil.getMsgCode();
        System.out.println("短信验证码："+msgCode);
        String phone= (String) map.get("phone");
        template.opsForValue().set(phone, msgCode, 10*60, TimeUnit.SECONDS);
        try {
            SendMessage.sendSms(phone,msgCode);
            return success();
        } catch (ClientException e) {
            e.printStackTrace();
            return errorException(Constants.ERROR_CODE_SENDMESSAGE_ERROR);
        }
//        return success();

    }

    /**
     * 用户注册
     */
    @RequestMapping(value = "register",method = RequestMethod.POST)
    public ReturnCode register(@RequestBody Map<String, String> map){
        String phone=map.get("phone");
        String realName=map.get("realName");
//        String code=map.get("code");  //取消手机验证
        String openId=map.get("openId");
        String idNum=map.get("idNum"); //修改身份认证
        IdCard idCard= IdCardUtil.getIdCardInfo(idNum);
        if(idCard==null){
            return errorException(Constants.ERROR_CODE_IDCARD_ERROR);
        }else{
            UserInfo userInfo=userInfoService.getUserInfoByOpenId(openId);
            if(userInfo!=null){
                String info= idCard.getStyle_simcall();
                String pro=info.split(",")[1];
                userInfo.setTelphone(phone);
                userInfo.setRealName(realName);
                userInfo.setStatus(DefineUtil.CHECK_STATUS);//认证通过
                userInfo.setIdNum(idNum);
                userInfo.setCheckDate(new Date());
                userInfo.setProvince(pro);
                userInfoService.update(userInfo);
                return success();
            }else{
                return errorException(Constants.ERROR_CODE_MEMBER_ERROR);
            }
        }
//        ValueOperations<String, String> forValue = template.opsForValue();
//        String oldCode = forValue.get(phone);
//        if(oldCode==null){
//            return errorException(Constants.ERROR_CODE_MSGCODE_ERROR);
//        }else if(oldCode.equals(code)){
//            //TODO
//            UserInfo userInfo=userInfoService.getUserInfoByOpenId(openId);
//            if(userInfo!=null){
//                userInfo.setTelphone(phone);
//                userInfo.setRealName(realName);
//                userInfo.setStatus("1");//认证通过
//                userInfo.setCheckDate(new Date());
//                userInfoService.update(userInfo);
//                return success();
//            }else{
//                return errorException(Constants.ERROR_CODE_MEMBER_ERROR);
//            }
//
//        }else{
//            return errorException(Constants.ERROR_CODE_MSGCODE_NULL_OR_TIMEOUT);
//        }
    }

    /**
     * 获取用户任务完成成功
     */
    @RequestMapping(value = "task",method = RequestMethod.GET)
    public ReturnCode taskInfo(String openId){
        UserInfo userInfo= userInfoService.getUserInfoByOpenId(openId);
        if(userInfo!=null){
            Map<String,Object> map=new HashMap<>();
            map.put("follow",userInfo.getFollow());
            map.put("status",userInfo.getStatus());
            map.put("num",userInfo.getInviteCount());
            return success(map);
        }
        return errorException(Constants.ERROR_CODE_MEMBER_ERROR);

    }





}
